package com.nj.dms.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nj.dms.bean.FileBean;
import com.nj.dms.bean.PageBean;
import com.nj.dms.bean.PaperMoldDocument;
import com.nj.dms.dao.FileDao;
import com.nj.dms.dao.PaperMoldDocumentDao;
import com.nj.dms.dao.impl.FileDaoImpl;
import com.nj.dms.dao.impl.PaperMoldDocumentDaoImpl;

/**
 * 我的文件列表
 */
@WebServlet("/MyFileList")
public class MyFileList extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 获取用户名
		String userName = (String) request.getSession().getAttribute("userName");

		// 获取页面传递过来的要显示的当前页的值
		String scurPage = request.getParameter("curPage");

		// 设置每页显示的最大记录数
		int maxSize = 13;

		if (userName != null) {

			// curPage表示要显示的页[当前页],默认值是1
			int curPage = 1;
			if (scurPage != null) {
				curPage = Integer.parseInt(scurPage.trim());
			}

			// 计算显示记录的开始位置 开始位置 = (要显示的页 - 1) * 每页显示的最大数量
			Integer beginIndex = (curPage - 1) * maxSize;

			FileDao fileDao = new FileDaoImpl();

			// 获取记录数量
			int recordNums = fileDao.countFilesByUser(userName);

			PageBean pageBean = new PageBean();

			pageBean.init(curPage, maxSize, recordNums);

			// 获取目标数据
			List<FileBean> fileList = fileDao.findAllFileByUserByPage(userName, beginIndex, maxSize);

			if (fileList != null) {

				// 文档集合不为空，跳转到列表

				request.setAttribute("pageBean", pageBean);

				request.setAttribute("fileList", fileList);

				request.getRequestDispatcher("document/my-file-list.jsp").forward(request, response);

			} else {

				// 文档集合为空，跳转到我的文档目录

				response.sendRedirect("document/my-document-menu.jsp");

			}

		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)

			throws ServletException, IOException {
		doGet(request, response);
	}

}
